# OpenCV 图像阈值化处理（二进制阈值化、反二进制阈值化、截断阈值化、反阈值化为0 和 阈值化为0）

import cv2
import numpy as np
import matplotlib.pyplot as plt

# 读取图片
src = cv2.imread("src/lena.png", cv2.IMREAD_UNCHANGED)
# 灰度图像处理
GrayImage = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)

# 1、二进制阈值化处理
r, b = cv2.threshold(GrayImage, 127, 255, cv2.THRESH_BINARY)
print(r)
# 显示图像
cv2.imshow("src", src)
cv2.imshow("result", b)
# 等待显示
cv2.waitKey(0)
cv2.destroyAllWindows()

# 2、反二进制阈值化处理
r, b = cv2.threshold(GrayImage, 127, 255, cv2.THRESH_BINARY_INV)
print(r)
# 显示图像
cv2.imshow("src", src)
cv2.imshow("result", b)
# 等待显示
cv2.waitKey(0)
cv2.destroyAllWindows()

# 3、截断阈值化处理
r, b = cv2.threshold(GrayImage, 127, 255, cv2.THRESH_TRUNC)
print(r)
# 显示图像
cv2.imshow("src", src)
cv2.imshow("result", b)
# 等待显示
cv2.waitKey(0)
cv2.destroyAllWindows()

# 4、反阈值化为0处理
r, b = cv2.threshold(GrayImage, 127, 255, cv2.THRESH_TOZERO_INV)
print(r)
# 显示图像
cv2.imshow("src", src)
cv2.imshow("result", b)
# 等待显示
cv2.waitKey(0)
cv2.destroyAllWindows()

# 5、阈值化为0处理
r, b = cv2.threshold(GrayImage, 127, 255, cv2.THRESH_TOZERO)
print(r)
# 显示图像
cv2.imshow("src", src)
cv2.imshow("result", b)
# 等待显示
cv2.waitKey(0)
cv2.destroyAllWindows()


# 读取图像（汇总显示）
img = cv2.imread("src/lena.png", cv2.IMREAD_UNCHANGED)
lenna_img = cv2.cvtColor(img ,cv2.COLOR_BGR2RGB)
GrayImage =cv2.cvtColor(img ,cv2.COLOR_BGR2GRAY)

# 阈值化处理
ret ,thresh1 =cv2.threshold(GrayImage ,127 ,255 ,cv2.THRESH_BINARY)
ret ,thresh2 =cv2.threshold(GrayImage ,127 ,255 ,cv2.THRESH_BINARY_INV)
ret ,thresh3 =cv2.threshold(GrayImage ,127 ,255 ,cv2.THRESH_TRUNC)
ret ,thresh4 =cv2.threshold(GrayImage ,127 ,255 ,cv2.THRESH_TOZERO)
ret ,thresh5 =cv2.threshold(GrayImage ,127 ,255 ,cv2.THRESH_TOZERO_INV)

# 显示结果
titles = ['Gray Image' ,'BINARY' ,'BINARY_INV' ,'TRUNC' ,'TOZERO' ,'TOZERO_INV']
images = [GrayImage, thresh1, thresh2, thresh3, thresh4, thresh5]
for i in range(6):
    plt.subplot(2 ,3 , i +1) ,plt.imshow(images[i] ,'gray')
    plt.title(titles[i])
    plt.xticks([]) ,plt.yticks([])
plt.show()